//
//  SDPhotoTagBaseCell.m
//  SKinDiary
//
//  Created by pcyang on 16/7/16.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDPhotoTagBaseCell.h"
#import "SDPhotoTagView.h"

#define kTagLeftPadding 10
#define kTagItemSpacing 5

#define kTopPadding 10

@interface SDPhotoTagBaseCell()
@property(nonatomic,strong)UIView* bottomLineView;

@end


@implementation SDPhotoTagBaseCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self initView];
    }
    return self;
}

-(void)initView
{
    self.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kTopPadding +5, 70, 13)];
    _titleLabel.font = [UIFont systemFontOfSize:13];
    _titleLabel.textColor = [UIColor colorWithRGB:0xb4bec8];
    _titleLabel.textAlignment = NSTextAlignmentRight;
    [_titleLabel sizeToFit];
    [self.contentView addSubview:_titleLabel];
    
    _bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(kTagLeftPadding, 0, kScreenWidth - 2*kTagLeftPadding, 1)];
    [_bottomLineView setBackgroundColor:[UIColor colorWithRGB:0xcad6e3]];
    [self.contentView addSubview:_bottomLineView];
    
    [self initData];
}

-(void)initData
{
    
}

+(NSMutableArray*)getTagItems
{
    return nil;
}

-(void)setCellData:(id)data
{
    NSArray* tagList = [data componentsSeparatedByString:NSLocalizedString(@",", nil)];
    for (NSString* tagName in tagList) {
        if (CHECK_VALID_STRING(tagName)) {
            [self.tagViewsArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                SDPhotoTagView* tagItemView  = (SDPhotoTagView*)obj;
                PhotoTagItem* tagItem  = [tagItemView getTagItem];
                if ([tagName isEqualToString:tagItem.title]) {
                    tagItem.inUsed = YES;
                }
                [tagItemView refresh];
            }];
        }
    }
}

-(void)layoutSubviews
{
    _titleLabel.right = 75;
    CGFloat leftPadding = _titleLabel.right + kTagLeftPadding;
    __block CGFloat offsetX = leftPadding;
    __block CGFloat offsetY = kTopPadding;
    [_tagViewsArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        SDPhotoTagView* tagItemView = (SDPhotoTagView*)obj;
        tagItemView.left = offsetX;
        tagItemView.top = offsetY;
        if (tagItemView.right > (kScreenWidth  - kTagItemSpacing))
        {
            offsetX = leftPadding;
            offsetY = tagItemView.bottom + kTopPadding;
            tagItemView.left = offsetX;
            tagItemView.top = offsetY;
            
            offsetX += tagItemView.width + kTagItemSpacing;
        }
        else
        {
            CGFloat a = tagItemView.width;
            offsetX += tagItemView.width + kTagItemSpacing;
        }
    }];
    
    _bottomLineView.top = self.height - 1;
}

-(NSArray*)getSelectedTagItems
{
    NSMutableArray* tagItems = [NSMutableArray arrayWithCapacity:0];
    for (SDPhotoTagView* tagItemView in self.tagViewsArray) {
        PhotoTagItem* tagItem  = [tagItemView getTagItem];
        if (tagItem.inUsed) {
            [tagItems addObject:tagItem.title];
        }
    }
    
    return tagItems;
}

+(float)getRequiredCellHeight:(NSArray*)tagItems
{
    __block float height = 0.0f;
    
    if (CHECK_VALID_ARRAY(tagItems)) {
        CGFloat leftPadding = 75 + kTagLeftPadding;
        __block CGFloat offsetX = leftPadding;
        __block CGFloat offsetY = kPhotoTagViewHeight + 2*kTopPadding;

        __block CGFloat width = 0.0f;
        [tagItems enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            PhotoTagItem* tagItem = (PhotoTagItem*)obj;
            CGSize size = [tagItem.title sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(MAXFLOAT,kPhotoTagViewHeight)];
            width = size.width + kPhotoTagPadding;
            CGFloat c = kScreenWidth;
            CGFloat a = offsetX + width + kTagItemSpacing;
            CGFloat b = kScreenWidth  - kTagItemSpacing;
            if ((offsetX + width + kTagItemSpacing) > (kScreenWidth  - kTagItemSpacing))
            {
                offsetX = leftPadding;
                offsetY +=  kPhotoTagViewHeight + kTopPadding;
            }
            
            {
                offsetX += width + kTagItemSpacing;
            }
        }];
        height = offsetY;
    }
    
    return height;
}

@end